package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.introspect.TypeResolutionContext;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;


    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    GraphInfoMapper graphInfoMapper;
    @Autowired
    ApartmentFacilityService apartmentFacilityService;
    @Autowired
    FacilityInfoMapper facilityInfoMapper;
    @Autowired
    ApartmentLabelService apartmentLabelService;
    @Autowired
    LabelInfoMapper labelInfoMapper;
    @Autowired
    ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    FeeValueMapper feeValueMapper;
    @Autowired
    RoomInfoMapper roomInfoMapper;

    @Override
    public void apartmentSaveOrUpdate(ApartmentSubmitVo apartmentSubmitVo) {
        //判断是插入还是更新
        boolean empty = apartmentSubmitVo.getId() != null;
        super.saveOrUpdate(apartmentSubmitVo);
        if (empty) {
            //更新的话选择是先把原来的列表给清空然后再插入，简化逻辑，后续可以优化
            //1.删除图片
            LambdaQueryWrapper<GraphInfo> graphWrapper = new LambdaQueryWrapper<>();
            graphWrapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId())
                    .eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(graphWrapper);
            //2.公寓配套
            LambdaQueryWrapper<ApartmentFacility> facilityWrapper = new LambdaQueryWrapper<>();
            facilityWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(facilityWrapper);
            //标签
            LambdaQueryWrapper<ApartmentLabel> labelWrapper = new LambdaQueryWrapper<>();
            labelWrapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(labelWrapper);
            //费用
            LambdaQueryWrapper<ApartmentFeeValue> feeWrapper = new LambdaQueryWrapper<>();
            feeWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(feeWrapper);
        }
        //图片插入
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graph = new GraphInfo();
                graph.setName(graphVo.getName());
                graph.setUrl(graphVo.getUrl());
                graph.setItemType(ItemType.APARTMENT);
                graph.setItemId(apartmentSubmitVo.getId());
                graphInfoList.add(graph);
            }
            graphInfoService.saveBatch(graphInfoList);
        }

        //配套插入
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        ArrayList<ApartmentFacility> facilityArraylist = new ArrayList<>();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            for (Long Id : facilityInfoIds) {
                ApartmentFacility build = ApartmentFacility.builder().apartmentId(apartmentSubmitVo.getId()).facilityId(Id).build();
                facilityArraylist.add(build);
            }
            apartmentFacilityService.saveBatch(facilityArraylist);
        }

        //标签插入
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
        if (!CollectionUtils.isEmpty(labelIds)) {
            for (Long Id : labelIds) {
                apartmentLabels.add(ApartmentLabel.builder().apartmentId(apartmentSubmitVo.getId()).labelId(Id).build());
            }
            apartmentLabelService.saveBatch(apartmentLabels);

        }

        //费用插入
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            for (Long Id : feeValueIds) {
                ApartmentFeeValue.builder().apartmentId(apartmentSubmitVo.getId()).feeValueId(Id).build();
                apartmentFeeValues.add(ApartmentFeeValue.builder().apartmentId(apartmentSubmitVo.getId()).feeValueId(Id).build());
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValues);
        }

    }


    //分页查询实现
    @Override
    public IPage<ApartmentItemVo> pageItem(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {

        return apartmentInfoMapper.pageItem(page, queryVo);


    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        //1. 查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);

        //2.查询图片列表
        ItemType item = ItemType.APARTMENT;
        List<GraphVo> graphVoList = graphInfoMapper.selectByItemTypeAndId(item, id);
        //3.查询标签
        List<LabelInfo> labelInfos = labelInfoMapper.selectLabels(id);
        //4.查询配套
        List<FacilityInfo> facilityInfos = facilityInfoMapper.selectFacilities(id);
        //5.查询杂费
        List<FeeValueVo> feeValueVos = feeValueMapper.selectFeeValues(id);
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfos);
        apartmentDetailVo.setFacilityInfoList(facilityInfos);
        apartmentDetailVo.setFeeValueVoList(feeValueVos);
        return apartmentDetailVo;
    }

    @Override
    public void myRemove(Long id) {
        //公寓的房间处理
        LambdaQueryWrapper<RoomInfo> roomInfoWrapper = new LambdaQueryWrapper<>();
        roomInfoWrapper.eq(RoomInfo::getApartmentId, id);
        Long count = roomInfoMapper.selectCount(roomInfoWrapper);
        if (count > 0) {
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

        apartmentInfoMapper.deleteById(id);
        //1.删除图片
        LambdaQueryWrapper<GraphInfo> graphWrapper = new LambdaQueryWrapper<>();
        graphWrapper.eq(GraphInfo::getItemId, id)
                .eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoService.remove(graphWrapper);
        //2.公寓配套
        LambdaQueryWrapper<ApartmentFacility> facilityWrapper = new LambdaQueryWrapper<>();
        facilityWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(facilityWrapper);
        //标签
        LambdaQueryWrapper<ApartmentLabel> labelWrapper = new LambdaQueryWrapper<>();
        labelWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(labelWrapper);
        //费用
        LambdaQueryWrapper<ApartmentFeeValue> feeWrapper = new LambdaQueryWrapper<>();
        feeWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(feeWrapper);


    }
}




